package com.scoy.honeymei.activity.mall;

import android.os.Bundle;
import android.view.View;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.model.HttpParams;
import com.scoy.honeymei.R;
import com.scoy.honeymei.adapter.ShopTicketAdapter;
import com.scoy.honeymei.base.BaseActivity;
import com.scoy.honeymei.bean.CouponBean0;
import com.scoy.honeymei.bean.ShopTicketBean;
import com.scoy.honeymei.databinding.CustomActivityList0Binding;
import com.scoy.honeymei.http.HpGo;
import com.scoy.honeymei.url.ApiCallBack;
import com.scoy.honeymei.url.MeConstant;
import com.scoy.honeymei.url.MyUrl;
import com.scoy.honeymei.utils.MyUtil;
import com.scoy.honeymei.utils.SPHelper;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName: CouponActivity$
 * @Description: 优惠券界面
 * @Author: GaoZhan
 * @CreateDate: 2019/9/2/0002$ 下午 14:26$
 * @UpdateUser: 更新者
 * @UpdateDate: 2019/9/2/0002$ 下午 14:26$
 * @UpdateRemark: 更新内容
 */
public class CouponShopActivity extends BaseActivity {


    private CouponShopActivity mContext;
    private ArrayList<CouponBean0> datalist;
    private ShopTicketAdapter couponListAdapter;
    private int pageInt;
    private CustomActivityList0Binding binding;
    private int shopId;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = CustomActivityList0Binding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        mContext = CouponShopActivity.this;

        shopId = getIntent().getIntExtra("shopId", 0);

        initNormal();

        httpTravelList(0);

    }

    @Override
    public void initNormal() {
        binding.layTop.titleTv.setText("优惠券");

        MyUtil.setStatusBar(mContext, getWindow(), true, R.color.colorWhite);
        binding.layTop.titleLlt.setPadding(0, MyUtil.getStatusBarHeight(mContext), 0, 0);

        datalist = new ArrayList<>();
        initRv();
      /*  binding.norSrl.setOnRefreshListener(refreshLayout -> {
            refreshLayout.finishRefresh(1500);
            datalist.clear();
            httpTravelList(0);

        }).setOnLoadMoreListener(refreshLayout -> {
            refreshLayout.finishLoadMore(1500);
            httpTravelList(pageInt);

        });*/

        binding.norSrl.setEnableRefresh(false).setEnableLoadMore(false);

        binding.layTop.backIv.setOnClickListener(v -> onBackPressed());

    }

    private void initRv() {
        couponListAdapter = new ShopTicketAdapter(mContext);
        binding.normalRv.setLayoutManager(new LinearLayoutManager(mContext, RecyclerView.VERTICAL, false));
        binding.normalRv.setAdapter(couponListAdapter);
        couponListAdapter.setOnOneClick(pos -> {
            ShopTicketBean couponBean = couponListAdapter.getItem(pos);
            httpGot(couponBean.getId(), pos);
        });

    }


    private void httpTravelList(int myPage) {
        HttpParams params = new HttpParams();
        params.put("page", myPage);
        params.put("shop_id", shopId);
        params.put("user_id", SPHelper.getString(MeConstant.UID, ""));
        HpGo.newInstance().HttpGo(this, MyUrl.SHOP_COUPON_LIST, params, new ApiCallBack() {
            @Override
            public void onSuccess(String o) {
                List<ShopTicketBean> data = new Gson().fromJson(o, new TypeToken<List<ShopTicketBean>>() {
                }.getType());
                couponListAdapter.setNewData(data);
                binding.nodataTv.setVisibility(couponListAdapter.getItemCount() == 0 ? View.VISIBLE : View.GONE);

            /*    pageInt = myPage + 1;
                datalist.addAll(data);
                couponListAdapter.notifyDataSetChanged();*/
            }

        });

    }

    //领取
    private void httpGot(int cId, int pos) {
        HttpParams params = new HttpParams();
        params.put("voucher_id", cId);
        params.put("user_id", SPHelper.getString(MeConstant.UID, ""));
        HpGo.newInstance().HttpGo(this, MyUrl.SHOP_COUPON_GOT, params, new ApiCallBack() {
            @Override
            public void onSuccess(String o) {
                binding.norSrl.autoRefresh();
                couponListAdapter.deletItem(pos);
                binding.nodataTv.setVisibility(couponListAdapter.getItemCount() == 0 ? View.VISIBLE : View.GONE);
                MyUtil.mytoast0(mContext, "已成功领取", R.mipmap.ic_sure);
            }
        });

    }
}
